/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.naga.bulksender.service;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.naga.bulksender.binding.Device;
import org.naga.bulksender.binding.DeviceConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 *
 * @author Naga Services
 *
 * A XML Implementation of Device Configuration througth JAXB
 */
public class XmlDeviceConfig implements DeviceConfiguration {
    private Log log = LogFactory.getLog(XmlDeviceConfig.class);
    
    public List<Device> getDevices() {
        log.debug("Get the Device's List");
      
        DeviceConfig config = new DeviceConfig();
        try {
            javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(DeviceConfig.class.getPackage().getName());
            javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();

            InputStream in = new FileInputStream("Device-Config.xml");
            if (in == null) {
                log.error("File No Found");
                return null;
            }

            config = (DeviceConfig) unmarshaller.unmarshal(in); //NOI18N

        } catch (Exception ex) {
            // XXXTODO Handle exception
            log.error("JAXB Exception during Load the XML File");
        }

        return config.getDevice();
    }
    /*
    public static void readObjectFromXml() {
    Devices devs = null;

    try {
    javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(Devices.class.getPackage().getName());
    javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();

    InputStream in = new FileInputStream("Device-Config.xml");
    if(in == null) {
    System.out.println("File No Found");
    return;
    }

    devs = (Devices)unmarshaller.unmarshal(in); //NOI18N
    System.out.println("Value" + devs.getDevice().size());
    } catch (Exception ex) {
    // XXXTODO Handle exception
    java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
    }

    }*/
    /*
    public static void persistXml() {
    DeviceConfig devs = new DeviceConfig();
    Device device = new Device();

    device.setModemId("modem1");
    device.setComPort("com3");
    device.setBaudRate( BigInteger.valueOf(100));
    device.setManufacturer("Nokia");
    device.setModel("3220");

    devs.getDevice().add(device);

    device = new Device();

    device.setModemId("modem5");
    device.setComPort("com5");
    device.setBaudRate( BigInteger.valueOf(500));
    device.setManufacturer("Motorola");
    device.setModel("6101");

    devs.getDevice().add(device);

    try {
    javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(devs.getClass().getPackage().getName());
    javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
    marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
    marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    //marshaller.marshal(devs, System.out);
    FileOutputStream salida = new FileOutputStream("Device-Config.xml");
    marshaller.marshal(devs, salida);
    } catch (Exception ex) {
    // XXXTODO Handle exception
    java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
    }
    }
     */

    public static void main(String[] args) {
        //      persistXml();
    }
}
